package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 分类分页查询
     * 
 * @param categoryPageQueryDTO
     * @return 
     * @author heiheiheng
     * @create 2025/7/16
     **/
    @Override
    public PageResult PageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;
    }

    /**
     * 修改分类
     * 
 * @param categoryDTO
     * @return 
     * @author heiheiheng
     * @create 2025/7/16
     **/
    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.updateCategory(category);
    }

    /**
     * 启用禁用分类
     * 
 * @param status
 * @param id
     * @return 
     * @author heiheiheng
     * @create 2025/7/16
     **/
    @Override
    public void startOrStop(Integer status, long id) {
        Category category = Category.builder().id(id).status(status).build();
        categoryMapper.updateCategory(category);
    }

    /**
     * 新增分类
     * 
 * @param categoryDTO
     * @return 
     * @author heiheiheng
     * @create 2025/7/17
     **/
    @Override
    public void saveCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(StatusConstant.DISABLE);
        categoryMapper.saveCategory(category);
    }

    /**
     * 删除分类
     *
 * @param  id
     * @return
     * @author heiheiheng
     * @create 2025/7/17
     **/
    @Override
    public void deleteCategoryByid(Long id) {
        categoryMapper.deleteCategoryByid(id);
    }

    /**
     * 根据类型查询
     *
 * @param type
     * @return
     * @author heiheiheng
     * @create 2025/7/17
     **/
    @Override
    public List<Category> queryCategoryBytype(Integer type) {
        CategoryDTO categoryDTO = new CategoryDTO();
        categoryDTO.setType(type);
        List<Category> categories = categoryMapper.queryCategoryBytype(categoryDTO);
        return categories;
    }
}
